package com.newsinfo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.newsinfo.mapper.ArticleMapper;
import com.newsinfo.pojo.Article;
import com.newsinfo.pojo.Category;
import com.newsinfo.pojo.PageBean;
import com.newsinfo.pojo.Result;
import com.newsinfo.service.ArticleService;
import com.newsinfo.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 新增文章
     * @param article
     * @return
     */
    @Override
    public Result add(Article article) {
        //判断是否存在该文章标题
        String title = article.getTitle();
        List<String> articleList = tileList();
        for (String s : articleList) {
            if (s.equals(title)) {
                return Result.error("已存在该文章标题");
            }
        }
        //从ThreadLocal中获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
        return Result.success();
    }

    /**
     * 文章分页查询
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //创建PageBean对象
        PageBean<Article> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用Mapper
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Article> articleList = articleMapper.list(categoryId,state,userId);
        Page<Article> page = (Page<Article>) articleList;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @Override
    public Result update(Article article) {
        //判断是否存在该文章标题
        String title = article.getTitle();
        List<String> articleList = tileList();
        for (String s : articleList) {
            if (s.equals(title)) {
                return Result.error("已存在该文章标题");
            }
        }
        articleMapper.update(article);
        return Result.success();
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @Override
    public Result<Article> detail(Integer id) {
        Article article = articleMapper.findById(id);
        if (article == null){
            return Result.error("无数据");
        }
        return Result.success(article);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        articleMapper.delete(id);
        return Result.success();
    }

    private List<String> tileList() {
        return articleMapper.tileList();
    }
}
